﻿$(function () {
    //announcement
    var msgBox = new jSharp.popUpDiv('divMsg');
    var productionID = $('[id$=hdfpid]').val();
    var argsType = 'ft'; //参数类型
    var list = document.getElementById('sortableArgs');
    //function
    function load() {
        //get
        $.get('../SellManage/SetOrder.aspx', {
            handler: 'load',
            pid: productionID,
            type: argsType
        }, function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                $(list).empty();
                for (var i = 0; i < o.length; i++) {
                    //createElement 
                    var _li = document.createElement('li');
                    $(_li).attr('args', o[i]["ID"].toString()).addClass("sortItem").html(o[i]["name"].toString());
                    $(list).append($(_li));
                }
                //initEffects
                initList();
            }
        })
    }
    function tips(s) {
        $('#msgContent').html(s);
        msgBox.Show();
    }
    function initList() {
        new Sortable(list, {
            group: 'args',
            onEnd: handler,
            onAdd: handler
        })
        function handler() {
            tips("重新排序中...");
            var itemOrder = '';
            $(list).children().each(function (index, elt) {
                itemOrder += $(elt).attr('args') + '|';
            })
            if (itemOrder != '') {
                itemOrder = itemOrder.substr(0, itemOrder.length - 1);
            }
            //回传
            $.post('../SellManage/SetOrder.aspx', {
                handler: 'set',
                type: argsType,
                data: itemOrder
            }, function (data) {
                msgBox.Close();
                if (data == "OK") {
                    tips("排序成功！");
                }
                else {
                    tips("排序失败！");
                }
                var t = setTimeout(function () {
                    msgBox.Close();
                    clearTimeout(t);
                }, 1000)
            })
        }


    }
    //handler
    $('#hideMsg').click(function () {
        msgBox.Close();
    })
    $('.argsType').each(function (index, elt) {
        $(elt).button().click(function () {
            argsType = $(elt).attr('args');
            $('.argsType').removeClass('buttonChecked');
            $(elt).addClass('buttonChecked');
            load();
        })
    })
    //init
    load();
} ())